package org.xyl.game.tool;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class GsonUtil {
    private volatile static GsonBuilder gsonBuilder;

    private GsonUtil() {
    }

    ;

    public static GsonBuilder getGsonBuilder() {
        if (gsonBuilder == null) {
            synchronized (GsonUtil.class) {
                if (gsonBuilder == null) {
                    gsonBuilder = new GsonBuilder();
                }
            }
        }
        return gsonBuilder;
    }
}
/*
作者：匿名用户
链接：https://www.zhihu.com/question/29398420/answer/140607711
来源：知乎
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

private static Gson gson = new GsonBuilder()
            .registerTypeAdapter(
                    new TypeToken<TreeMap<String, Object>>(){}.getType(),
                    new JsonDeserializer<TreeMap<String, Object>>() {
                        @Override
                        public TreeMap<String, Object> deserialize(
                                JsonElement json, Type typeOfT,
                                JsonDeserializationContext context) throws JsonParseException {

                            TreeMap<String, Object> treeMap = new TreeMap<>();
                            JsonObject jsonObject = json.getAsJsonObject();
                            Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
                            for (Map.Entry<String, JsonElement> entry : entrySet) {
                                Object ot = entry.getValue();
                                if(ot instanceof JsonPrimitive){
                                    treeMap.put(entry.getKey(), ((JsonPrimitive) ot).getAsString());
                                }else{
                                    treeMap.put(entry.getKey(), ot);
                                }
                            }
                            return treeMap;
                        }
                    }).create();
    private static Type typeToken = new TypeToken<TreeMap<String, Object>>(){}.getType();

 */